package com.ordersystem.controller;

import com.ordersystem.domain.Result;
import com.ordersystem.domain.Role;
import com.ordersystem.service.EmployeeService;
import com.ordersystem.service.RoleService;
import com.ordersystem.utils.PageUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : asus1020
 * @version : [v1.0]
 * @className : RoleController
 * @description : [描述说明该类的功能]
 * @createTime : [2022/3/25 17:06]
 * @updateUser : asus1020
 * @updateTime : [2022/3/25 17:06]
 * @updateRemark : [描述说明本次修改内容]
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private EmployeeService employeeService;
    PageUtils pageUtils=new PageUtils();
    @GetMapping("/findRoleList")//查询角色列表
    public Result findRoleList(){
        Result result = new Result();
        List<Role> roleList = roleService.findRoleList();
        if (roleList!=null){
            result.setMessage("查询角色列表成功");
            result.setStatus(200);
            result.setData(roleList);
        }else {
            result.setStatus(500);
            result.setMessage("查询角色列表失败");
        }

        return result;
    }

    @GetMapping("/role/{rid}")//根据id查询单个角色
    public Result findRoleById(@PathVariable("rid")Integer rid){
        Result result = new Result();
        Role role = roleService.findRoleById(rid);
        if (role!=null){
            result.setMessage("查询单个角色成功");
            result.setStatus(200);
            result.setData(role);
        }else {
            result.setStatus(500);
            result.setMessage("查询单个角色失败");
        }
        return result;
    }

    @PutMapping("/updateRole")//修改角色
    public Result updateRole(@RequestBody Role role){
        Result result = new Result();
        int total = roleService.updateRole(role);
        if (total>0){
            Role roleById = roleService.findRoleById(role.getRid());
            result.setMessage("修改用户成功");
            result.setStatus(200);
            result.setData(roleById);
        }else {
            result.setMessage("修改用户失败");
            result.setStatus(200);
        }

        return result;
    }

    @PostMapping("/addRole")
    public Result addRole(@RequestBody Role role){
        Result result = new Result();
        int total = roleService.addRole(role);
        if (total>0){
            result.setMessage("添加角色成功");
            result.setStatus(200);
            result.setData(roleService.findRoleById(role.getRid()));
        }else{
            result.setStatus(500);
            result.setMessage("添加角色失败");
        }
        return result;

    }

    @DeleteMapping("/deleteRole/{rid}")
    public Result deleteRole(@PathVariable("rid") Integer rid){
        Result result = new Result();
        Integer employeeNumberByRid = employeeService.statisticsEmployeeNumberByRid(rid);
        if (employeeNumberByRid>0){
            return Result.fail("该角色下已绑定用户，请先结绑！");
        }
        int total = roleService.deleteRole(rid);
        if(total>0){
            result.setStatus(200);
            result.setMessage("删除成功");
        }else {
            result.setStatus(500);
            result.setMessage("删除角色失败");
        }

        return result;
    }

}
